
const { tips, coludRequest,confirm } = require("../../util/util");
const util = require("../../util/util");
 
const app=new getApp()
// pages/take/detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null, //用户信息
    status:'待取餐',
    address:"",
    cart:'',
    sumPrice:'',
    orderCode:'',  
    createTime:'',
    remark:'没有备注',
    Pick_up_code:''   //取货码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.caozuo();
    //this.GetUser();
     
    

    

  },
  //清空购物车缓存数据
  clearCart:function(){
    wx.removeStorage({
      key:'cart',
      success:function(res){
        //清空成功
        tips('下单成功');
      }
    })
  },
  //生成订单
  caozuo:function(){
    var that=this;
    let cart=wx.getStorageSync('cart') || [];
        if(cart.length>0){
          //获取该用户的第一条地址
          wx.cloud.callFunction({
            name:'login',
            data:{},
            success:res=>{
              let openId=res.result.userInfo.openId;
              coludRequest('admin',{module:'UserAddress',action:'query',map:{openId:openId}},function(res)
              {
                  var arr=res.result.data;
                  that.setData({address:arr[0]})
                  //获取购物车信息
                 let cart=wx.getStorageSync('cart');
              
                  //总价
                  let sumPrice=0
                  for(let i=0;i<cart.length;i++){
                    sumPrice=sumPrice+(cart[i].sub_price*cart[i].num)
                  }
                  //订单编号
                  var timestamp=Date.parse(new Date());
                  timestamp=timestamp/1000;
                  let code=timestamp+timestamp+timestamp+'ABC'
                  console.log(code);
                  //下单时间
                  let creatime=util.formatTime(new Date());
                  //取货码
                  let ran="";
                  for(let i=0;i<4;i++){
                    ran+=Math.floor(Math.random()*10)
                  }
                  //绑定数据
                  that.setData({
                    sumPrice:sumPrice,
                    cart,
                    orderCode:code,
                    createTime:creatime,
                    Pick_up_code:ran
                  })
                  //地址
                  let UserAddress=arr[0]._id;
                  //给用户新增订单
                  let orderData=that.data;
                  //数据验证通过后开始添加数据
                  coludRequest('admin',{module:'Order',action:'add',params:{
                    Amount:orderData.sumPrice,
                    Code:code,
                    CreateTime:orderData.createTime,
                    ModifyTime:orderData.createTime,
                    Remark:orderData.remark,
                    Status:orderData.status,
                    UserAddressId:orderData.address._id,
                    Pick_up_code:that.data.Pick_up_code,
                    cart:cart,
                    UserOpenId:openId,
                    address:UserAddress
                  }},function(res){
                    //调用清空购物车方法
                    that.clearCart();
                    
                  })
              })
              
              
            }

          })
          //有数据是将数据渲染到页面并生成订单表
          
        }else{
          tips('发生了错误')
        }
  },
  //电话客服
  dial:function(){
    wx.makePhoneCall({
      phoneNumber: '15973798767', //仅为示例，并非真实的电话号码,
      fail:err=>{
        tips('你取消了电话客服')
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})